import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/UserLogin",
  },
  {
    path: "/home",
    name: "HomePage",
    component: () => import("../views/HomePage.vue"),
    meta: {
      requiresAuth: true,
    },
  },
  {
    path: "/UserLogin",
    name: "UserLogin",
    component: () => import("../views/UserLogin.vue"),
  },
  {
    path: "/login",
    redirect: "/UserLogin",
  },

];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = sessionStorage.getItem("token");

  if (to.meta.requiresAuth && !isAuthenticated) {
    next("/UserLogin");
  } else {
    next();
  }
});

export default router;
